package com.wintop.youyuan.dao.impl;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.stereotype.Repository;

import com.wintop.youyuan.dao.UserDao;
import com.wintop.youyuan.model.BaseData;
import com.wintop.youyuan.model.DetailData;
import com.wintop.youyuan.model.PersonalsCondition;
import com.wintop.youyuan.model.User;
import com.wintop.youyuan.util.MD5Util;

@Repository("userDao")
public class UserDaoImpl implements UserDao {
	private SessionFactory sessionFactory;

	@Resource(name = "sessionFactory")
	public void setSessionFactory(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}

	@Override
	public boolean addUser(User user, String birthday) {
		user.setTime(new Date(System.currentTimeMillis()));
		user.setPassword(MD5Util.md5Encode(user.getPassword()));
		Session session = sessionFactory.getCurrentSession();
		BaseData baseData = new BaseData(0, user.getUserid(), null, 0, 0,
				0, 0, 0, 0, 0, null, 0, birthday, null, null);
		session.save(baseData);
		DetailData detailData = new DetailData(0, user.getUserid(), 0, 0, 0, 0,
				0, 0, null, null, null, null);
		session.save(detailData);
		PersonalsCondition personalsCondition = new PersonalsCondition(0,
				user.getUserid(), null, 0, 0, 0, 0, 0, 0);
		session.save(personalsCondition);

		session.save(user);
		return true;
	}

	@Override
	public User getUser(String userid, String password) {
		Session session = sessionFactory.getCurrentSession();
		String hql = "from User where userid=:u and password=:p";
		Query q = session.createQuery(hql);
		q.setString("u", userid);
		q.setString("p", MD5Util.md5Encode(password));
		User user = (User) q.uniqueResult();
		return user;
	}

	@Override
	public User getUser(String userid) {
		Session session = sessionFactory.getCurrentSession();
		Query query = session.createQuery("from User where userid=:id");
		query.setString("id", userid);
		return (User) query.uniqueResult();
	}

	@Override
	public List<User> getUsers() {
		Session session = sessionFactory.getCurrentSession();
		Query query = session.createQuery("from User");
		@SuppressWarnings("unchecked")
		List<User> users = query.list();
		return users;
	}

	@Override
	public boolean updateUser(User user) {
		Session session = sessionFactory.getCurrentSession();
		session.update(user);
		session.flush();
		return true;
	}

	@Override
	public boolean deleteUser(int id) {
		Session session = sessionFactory.getCurrentSession();
		User user = (User) session.get(User.class, id);
		session.delete(user);
		Query query1 = session.createQuery("delete BaseData where userid=:id");
		query1.setString("id", user.getUserid());
		query1.executeUpdate();
		Query query2 = session
				.createQuery("delete DetailData where userid=:id");
		query2.setString("id", user.getUserid());
		query2.executeUpdate();
		Query query3 = session
				.createQuery("delete PersonalsCondition where userid=:id");
		query3.setString("id", user.getUserid());
		query3.executeUpdate();
		return true;
	}

	@Override
	public User getUser(int id) {
		Session session = sessionFactory.getCurrentSession();
		return (User) session.get(User.class, id);
	}

}
